package 
{
	import flash.display.BitmapData;
	import flash.display.GraphicsEndFill;
	import flash.display.GraphicsGradientFill;
	import flash.display.GraphicsPath;
	import flash.display.GraphicsSolidFill;
	import flash.display.GraphicsStroke;
	import flash.display.IGraphicsData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.net.registerClassAlias;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.CompressionAlgorithm;
	import flash.utils.getTimer;
	
	/**
	 * ...
	 * @author Lorenzo Nuvoletta
	 */
	public class Main extends Sprite 
	{
		private var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			stage.addEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function onClick(e:MouseEvent):void 
		{
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			var urlReq:URLRequest = new URLRequest("mixed.swf");
			loader.load(urlReq, loaderContext);
		}
		
		private function onComplete(e:Event):void 
		{
			addChild(LoaderInfo(e.target).content);
			LoaderInfo(e.target).content.x = Math.random() * stage.width;
			LoaderInfo(e.target).content.y = Math.random() * stage.height;			
			
			//-------------------
			var vectorGraphic:Vector.<IGraphicsData> = Sprite(LoaderInfo(e.target).content).graphics.readGraphicsData(true);
			
			var bounds:Rectangle = Sprite(LoaderInfo(e.target).content).getBounds(null);
			
			var codec:VectorGraphics = new VectorGraphics();
			var bytes:ByteArray = codec.writeToBytes(vectorGraphic, bounds);
			saveFile(bytes, "vectorUncompressed.txt");
			trace(bytes.length);
			
			var baTest:ByteArray = new ByteArray();
			baTest.writeFloat(123.345467);
			baTest.writeFloat(432.0023400234234);
			saveFile(baTest, "testFloat.txt");
			
			//time test
			var time:Number = getTimer();
			bytes.compress(CompressionAlgorithm.LZMA);
			trace("Compress LZMA " + String(getTimer() - time));
			trace("Size is " + bytes.length);
			time = getTimer();
			bytes.uncompress(CompressionAlgorithm.LZMA);
			trace("UNncompress LZMA " + String(getTimer() - time));
			
			time = getTimer();
			bytes.compress(CompressionAlgorithm.ZLIB);
			trace("Compress ZLIB " + String(getTimer() - time));
			trace("Size is " + bytes.length);
			time = getTimer();
			bytes.uncompress(CompressionAlgorithm.ZLIB);
			trace("UNcompress ZLIB " + String(getTimer() - time));
			
			time = getTimer();
			bytes.compress(CompressionAlgorithm.DEFLATE);
			trace("Compress Deflate " + String(getTimer() - time));
			trace("Size is " + bytes.length);
			time = getTimer();
			bytes.uncompress(CompressionAlgorithm.DEFLATE);
			trace("UNcompress Deflate " + String(getTimer() - time));
			
			
			bytes.compress(CompressionAlgorithm.LZMA);
			trace(bytes.length);			
			
			registerClassAlias("flash.display.IGraphicsData", IGraphicsData);
			registerClassAlias("flash.display.GraphicsGradientFill", GraphicsGradientFill);
			registerClassAlias("flash.display.GraphicsEndFill", GraphicsEndFill);
			registerClassAlias("flash.display.GraphicsPath", GraphicsPath);
			registerClassAlias("flash.display.GraphicsSolidFill", GraphicsSolidFill);
			registerClassAlias("flash.display.GraphicsStroke", GraphicsStroke);
			registerClassAlias("flash.geom.Matrix", Matrix);
			
			var byteArray:ByteArray = new ByteArray();
			byteArray.writeObject(vectorGraphic);
			byteArray.compress(CompressionAlgorithm.LZMA);
			trace(byteArray.length);			
			
			saveFile(byteArray, "paris.txt");
			
			byteArray.uncompress(CompressionAlgorithm.LZMA);
			byteArray.position = 0;
			var v:Vector.<IGraphicsData> = byteArray.readObject() as Vector.<IGraphicsData>;
			bytes.uncompress(CompressionAlgorithm.LZMA);
			bytes.position = 0;
			v = codec.readFromBytes(bytes);			
			
			var sprite:Sprite = new Sprite();
			addChild(sprite);
			time = getTimer();
			sprite.graphics.drawGraphicsData(v);
			trace("sprite.graphics.drawGraphicsData(v);", getTimer() - time);
			
			time = getTimer();
			var bmp:BitmapData = new BitmapData(sprite.width, sprite.height, false);
			bmp.draw(sprite);
			trace("bmp.draw(sprite)", getTimer() - time);
			
			new CairoParser(v);
		}
		
		private function saveFile(byteArray:ByteArray, filename:String):void 
		{
			var path:String = File.applicationStorageDirectory.nativePath;				
			var file:File = new File(path);
			file = file.resolvePath(filename);
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.WRITE);
			fileStream.writeBytes(byteArray, 0, byteArray.length);
			fileStream.close();		
		}
		
	}
	
}